<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <button>发送请求</button>
    <button>取消请求</button>

    <script src="https://cdn.bootcdn.net/ajax/libs/axios/0.27.2/axios.min.js"></script>
    <script>
        let btn = document.getElementsByTagName('button')
        let cancleFn;
        btn[0].onclick=async ()=>{
          try {
            const response = await axios({
                url:'http://localhost:8090/delay-server',
                cancelToken:new axios.CancelToken((cancle)=>{
                    cancleFn=cancle;
                })
            })
            console.log(response);
          } catch (error) {
              if(axios.isCancel(error)){
                  alert('被取消了');
              }else{
                  alert(error.message);
              }
          }
        }
        btn[1].onclick=()=>{
            cancleFn();
        }
    </script>
</body>
</html>